package com.uva.pgpi.sgcc.preload;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.sql.SQLException;
import java.util.Iterator;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.support.ConnectionSource;
import com.uva.pgpi.sgcc.beans.Historico;
import com.uva.pgpi.sgcc.beans.Solicitud;
import com.uva.pgpi.sgcc.util.DataBaseConnection;
import com.uva.pgpi.sgcc.util.DateUtils;

public class CrearHistoricos {

	public static void listHistorico() throws SQLException {

		ConnectionSource con = DataBaseConnection.getInstance().getConnection();

		Dao<Historico, String> HistoricoDao = DaoManager.createDao(con,
				Historico.class);

		Iterator<Historico> it = HistoricoDao.queryForAll().iterator();

		while (it.hasNext()) {
			System.out.println(it.next().toString());
		}

	}

	public static void main(String[] args) throws SQLException,
			NoSuchAlgorithmException, InvalidKeySpecException {

		ConnectionSource connectionSource = DataBaseConnection.getInstance()
				.getConnection();

		Dao<Historico, String> historicoDao = DaoManager.createDao(
				connectionSource, Historico.class);

		Dao<Solicitud, String> solicitudDao = DaoManager.createDao(connectionSource,
				Solicitud.class);
		
		//Historico h = historicoDao.queryForId(19+"");
//		
//		String s = "21/12/2013 09:54";
//		String s2 = "21/12/2013 12:09";
//		
//		h.setDescripcion("Abierto -> Asignado["+DateUtils.getDuration(DateUtils.StringToLong(s2)-DateUtils.StringToLong(s))+"]");
//		
//		historicoDao.update(h);
		
//		DeleteBuilder<Historico, String> deleteBuilder =
//				historicoDao.deleteBuilder();
//		deleteBuilder.where().eq(Historico.SOLICITUD, 1);
//		deleteBuilder.delete();
//		
//		DeleteBuilder<Solicitud, String> deleteBuilderS =
//				solicitudDao.deleteBuilder();
//		deleteBuilderS.where().eq(Solicitud.ID_SOLICITUD, 1);
//		deleteBuilderS.delete();

		//TableUtils.clearTable(connectionSource, Historico.class);
		//TableUtils.clearTable(connectionSource, Solicitud.class);
		//TableUtils.createTableIfNotExists(connectionSource, Historico.class);

		//ArrayList<Historico> historico = new ArrayList<Historico>();

		//historico.add(new Historico(solicitudDao.queryForId("1"),s, System.currentTimeMillis()));

		//for (int i = 0; i < historico.size(); i++) {
			//historicoDao.createIfNotExists(historico.get(i));
		//}

		connectionSource.close();

		listHistorico();

	}

}
